// ----------------------------------------------------------------------------
// CSingleton.h
//
// This class can be used in any project that will need to use the Singleton
// design pattern.
// ----------------------------------------------------------------------------

#ifndef CSINGLETON_H
#define CSINGLETON_H

#include <cassert>

template <typename T> class CSingleton
{
	static T* ms_Singleton;

public:
	CSingleton(void)
	{
		assert(!ms_Singleton);
		int offset = (int)(T*)1 - (int)(CSingleton <T>*)(T*)1;
		ms_Singleton = (T*)((int)this + offset);
	}
	~CSingleton(void)
		{ assert(ms_Singleton); ms_Singleton = 0; }

	static T& GetSingleton(void)
		{ assert (ms_Singleton); return ( *ms_Singleton ); }

	static T* GetSingletonPtr(void)
		{ return (ms_Singleton); }
};

template <typename T> T* CSingleton <T>::ms_Singleton = 0;

#endif
